package com.XinYun.Library.modules.person.Dao;

import cn.hutool.core.lang.Assert;
import cn.hutool.db.StatementUtil;
import com.XinYun.Library.modules.person.Entity.User;
import com.XinYun.Library.utils.SqlUtils.DruidUtil;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class SQLDao {
        private static void security(String sql) throws SQLException {
            //sql语句·检测，调用了HuTools的Assert
            Assert.notBlank(sql, "sql语句不能为空！");
            sql = sql.trim();
            String tag = new Scanner(sql).next();
            if(tag.equalsIgnoreCase("SELECT")||
                    tag.equalsIgnoreCase("UPDATE")||
                    tag.equalsIgnoreCase("INSERT")||
                    tag.equalsIgnoreCase("DELETE")) {
            } else {
                throw new SQLException("请不要输入CRUD以外语句");
            }
        }

        public ResultSet select(String sql,Object obj) throws SQLException, IOException {
            security(sql);
            Connection connection= DruidUtil.getConnection();
            ResultSet resultSet = null;
            try {
                PreparedStatement preparedStatement = connection.prepareStatement(sql);
                //若传过来obj不为空，则解析参数集合
                if (obj != null){

                        //对问号传参
                        preparedStatement.setObject(1,obj);

                }
                resultSet = preparedStatement.executeQuery(sql);
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
            return resultSet;
        }
    public List<User> getuser(String value) {
        SQLDao sql = new SQLDao();
        String Sql = "select * from user_info where uid = ?";
        String Sql1 = "update user_info set isbanned = ? where uid = ?";
        List<User> UserList = new ArrayList<>();
        {
            try {
                Object obj = value;
                ResultSet resultSet = sql.select(Sql,obj);
                if(resultSet.getInt("times")>=3){
                    update(Sql1,1,value);
                }
                User user = new User(resultSet.getString("uid"),resultSet.getString("username"),resultSet.getString("direction"),resultSet.getString("date"),resultSet.getString("intername"),resultSet.getString("sid"),resultSet.getString("password"),resultSet.getString("mail"),resultSet.getInt("times"),resultSet.getString("phoneNumber"),resultSet.getBoolean("isbanned"));

                UserList.add(user);
            } catch (Exception e) {
                e.printStackTrace();
            }finally {
                //关闭资源
                DruidUtil.close();
            }
        }
        return UserList;
    }
    public static int update(String sql,Object obj,String uid) throws SQLException {
        security(sql);
        Connection connection= DruidUtil.getConnection();
        //获取statement对象,绑定sql语句
        PreparedStatement ps = StatementUtil.prepareStatement(connection,sql);
        ps.setObject(1,obj);
        ps.setObject(2,uid);
        int line = ps.executeUpdate();
        return line;
    }
}
